import 'package:flutter/material.dart';
import 'package:flutter_mwm/base/base.dart';
import 'package:flutter_mwm/impl/http.dart';
import 'package:flutter_mwm/widget/button.dart';
import 'package:flutter_mwm/widget/edit.dart';
import '../widget/widget.dart';
import '../util/util.dart';
import '../impl/impl.dart';
import '../beans/bean.dart';

class RegisterHome extends BaseWidget<RegisterHome> {
  @override
  State<StatefulWidget> createState() {
    return _StateRegister();
  }
}

class _StateRegister extends StateBase<RegisterHome>
    implements HttpCallbackImpl {
  TextEditingController _accountController = TextEditingController();
  TextEditingController _passwordController = TextEditingController();
  TextEditingController _nicknameController = TextEditingController();
  TextEditingController _repeatController = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return PhotoWallpaper(
      context,
      title: '快速注册',
      child: _registerForm(),
    );
  }

  Widget _registerForm() {
    return ListView(
      shrinkWrap: true,
      children: <Widget>[
        _weight(),
        _inputForm(),
        _registerButton(),
        _weight(),
      ],
    );
  }

  Widget _weight() {
    return Flex(direction: Axis.vertical);
  }

  Widget _inputForm() {
    return Container(
        child: Container(
      margin: EdgeInsets.fromLTRB(10, 20, 10, 10),
      padding: EdgeInsets.symmetric(horizontal: 8.0),
      decoration: BoxDecoration(
        borderRadius: BorderRadius.all(Radius.circular(8.0)),
        color: Colors.white,
        border: Border.all(color: Color(0xFF003355), width: 3.0),
      ),
      child: Column(
        children: <Widget>[
          EditText(
            showClearIcon: true,
            labelText: '账号名称(仅限数字和字母)',
            controller: _accountController,
          ),
          _driverLine(),
          EditText(
            showClearIcon: true,
            labelText: '昵称(可不填，默认使用账号名称)',
            controller: _nicknameController,
          ),
          _driverLine(),
          EditText(
              showClearIcon: true,
              keyboardType: TextInputType.emailAddress,
              labelText: '用户密码',
              obscureText: true,
              controller: _passwordController),
          _driverLine(),
          EditText(
              showClearIcon: true,
              keyboardType: TextInputType.emailAddress,
              labelText: '再次输入密码',
              obscureText: true,
              controller: _repeatController),
        ],
      ),
    ));
  }

  Widget _registerButton() {
    return Row(children: <Widget>[
      ExpandedButton("立即注册",
          radius: 8.0,
          margin: EdgeInsets.symmetric(vertical: 15, horizontal: 10.0),
          padding: EdgeInsets.all(10.0),
          background: Color(0xFF3d6488),
          highlightBackground: Color(0xFF3d7999),
          textColor: Color(0xFF0099CC),
          highlightTextColor: Colors.white,
          textSize: 18.0,
          onPressed: _press2Register)
    ]);
  }

  Widget _driverLine() {
    return Line(color: Color(0xFF00CCCC));
  }

  void _press2Register() {
    String account = _accountController.text;
    if (TextUtils.isEmpty(account)) {
      showToast("请输入要注册的用户名！");
      return;
    }
    String password = _passwordController.text;
    if (TextUtils.isEmpty(password)) {
      showToast("请输入密码！");
      return;
    }
    String repeat = _repeatController.text;
    if (!TextUtils.equals(password, repeat)) {
      showToast("两次输入密码不一致");
      return;
    }
    String nickName = _nicknameController.text;
    AccountBean accountBean =
        AccountBean(username: account, password: password, nickname: nickName);
    Map<String, String> bodyParams = {
      'beanJson': GsonUtil.bean2Json(accountBean.toJson())
    };
    var http = HttpKit.getInstance(context: context, tag: account);
    http.callback(this);
    http.execute(method: 'register', paramBody: bodyParams);
  }

  @override
  void onResult(int what, Object tag, String result) {
    try {
      ReturnAccountBean resultBean = ReturnAccountBean.json2Bean(result);
      if (null == resultBean) {
        showPromptDialog("数据格式错误或未返回数据");
        return;
      }
      String code = resultBean.code;
      String message = resultBean.message;
      if (!TextUtils.equals("1", code)) {
        showPromptDialog(message);
      } else {
        showToast('注册成功！');
        String account = tag;
        Future.delayed(Duration(milliseconds: 1500), _setResult(account));
      }
    } catch (error) {
      onError(what, tag, error.toString());
    }
  }

  Null _setResult(String account) {
    Navigator.pop(context, account);
  }

  @override
  void onError(int what, Object tag, String error) {
    showErrorPromptDialog('', error);
  }
}
